package com.wcr.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wcr.school.mapper.AdminMapper;
import com.wcr.school.pojo.Admin;
import com.wcr.school.pojo.Clazz;
import com.wcr.school.pojo.Login;
import com.wcr.school.service.AdminService;
import com.wcr.school.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service("implAdminService")
@Transactional
public class ImplAdminService extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Override
    public Admin login(Login login) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",login.getUsername());
        queryWrapper.eq("password", MD5.encrypt(login.getPassword()));
        Admin admin = baseMapper.selectOne(queryWrapper);
        return admin;
    }

    @Override
    public Admin getAdminById(Long userId) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        Admin admin = baseMapper.selectOne(queryWrapper);
        return admin;
    }

    @Override
    public IPage<Admin> getAdminByOption(Page<Admin> page, String adminName) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(adminName))
            queryWrapper.like("name",adminName);
        queryWrapper.orderByAsc("id");
        Page page1 = baseMapper.selectPage(page,queryWrapper);
        return page1;
    }
}
